package prefetch

import (
	"log"
	"strings"
)

type Predictor struct {
	history map[string]int // 文件访问频率统计
}

func NewPredictor() *Predictor {
	return &Predictor{history: make(map[string]int)}
}

func (p *Predictor) Learn(path string) {
	p.history[path]++
}

func (p *Predictor) Predict(topN int) []string {
	type pair struct {
		Path string
		Cnt  int
	}
	var ranked []pair
	for k, v := range p.history {
		if strings.HasPrefix(k, "/usr") || strings.HasSuffix(k, ".so") {
			ranked = append(ranked, pair{k, v})
		}
	}
	// 简化排序逻辑：按访问次数筛选前N
	if len(ranked) > topN {
		ranked = ranked[:topN]
	}
	result := []string{}
	for _, r := range ranked {
		result = append(result, r.Path)
	}
	log.Printf("[predictor] predicted %d hot files", len(result))
	return result
}
